import tools from '../utils/tools'

export default class appBridge {
    constructor() {
        this.platform = 'app'
        this._readyPromise = new Promise(resolve => {
            document.addEventListener('UniAppJSBridgeReady', function () {
                resolve()
            });
        })
    }

    onReady() {
        return this._readyPromise
    }

    back(params) {
        uni.navigateBack(params);
    }

    navigateTo(params) {
        this.onReady().then(() => {
            uni.navigateTo(params);
        })
    }

    redirectTo(params) {
        uni.redirectTo(params);
    }

    reLaunch(params) {
        uni.reLaunch(params);
    }

    // 手动调起分享，小程序的话需要提前设置分享图，platform：h5 | mini
    share(params) {
        uni.postMessage({
            data: {
                type: 'share',
                platform: params.platform
            }
        });
    }
    // 长按调起保存
    downloadUrl(params) {
        uni.postMessage({
            data: {
                type: 'downloadUrl',
                params
            }
        });
    }
    preview(params) {
        uni.postMessage({
            data: {
                type: 'preview',
                params: params.urls
            }
        });
    }
    // 打开一个新的专题页面
    goZhuanti(url) {
        if (this.appVersionHigherThan('2.6.50')) {
            this.navigateTo({
                url: '/pages/richText/headlessWebview?url=' + encodeURIComponent(Config.host + url)
            })
        } else {
            this.navigateTo({
                url: '/pages/richText/webview?url=' + encodeURIComponent(Config.host + url)
            })
        }
    }

    // 更新分享信息
    updateShareData(params) {
        uni.postMessage({
            data: {
                type: 'updateShareData',
                ...params
            }
        });
    }

    // 设置右上角分享按钮的分享平台 h5/mini
    setSharePlatform(platform) {
        uni.postMessage({
            data: {
                type: 'setSharePlatform',
                platform: platform
            }
        });
    }

    // 更新页面标题，app无效果，只是为了统一api
    setTitle(title) {
        document.title = title
    }

    getUserInfo() {
        try {
            let storage = plus.storage.getItem('myUserInfo')
            if (storage) {
                return JSON.parse(storage)
            } else {
                return {
                    uid: ''
                }
            }
        } catch (e) {
            return {
                uid: ''
            }
        }
    }

    // 检查app版本是否高于某个版本
    appVersionHigherThan(version) {
        let nums = version.split('.')
        let versionCode = nums[0] + ((nums[1].length < 2 ? '0' : '') + nums[1]) + ((nums[2].length < 2 ? '00' : (nums[2].length < 3 ? '0' : '')) + nums[2])

        let current = this.getAppVersion()
        let currentNums = current.split('.')
        let currentVersionCode = currentNums[0] + ((currentNums[1].length < 2 ? '0' : '') + currentNums[1]) + ((currentNums[2].length < 2 ? '00' : (currentNums[2].length < 3 ? '0' : '')) + currentNums[2])

        return currentVersionCode > versionCode
    }

    getAppVersion() {
        return plus.runtime.version
    }
}
